package com.bw.module_home.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.bw.lib_mvicore.ui.BaseMviFragment
import com.bw.module_home.R
import com.bw.module_home.databinding.HomeFragmentHomeDetailCommentBinding
import com.bw.module_home.intent.CommentIntent
import com.bw.module_home.model.entity.SimpleVideoEntity
import com.bw.module_home.state.CommentState
import com.bw.module_home.viewmodel.CommentViewModel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class HomeDetailCommentFragment : BaseMviFragment() {

    private lateinit var binding: HomeFragmentHomeDetailCommentBinding
    lateinit var viewModel: CommentViewModel
    lateinit var data: SimpleVideoEntity

    override fun generateViewModel() {
        viewModel = ViewModelProvider(this)[CommentViewModel::class.java]
    }

    override fun initEvent() {
        binding.btnHomeDetailCommentPublish.setOnClickListener{
            //获取用户输入的评论内容
            val commentContent = binding.etHomeDetailCommentContent.text.trim().toString()
            if (commentContent.isBlank()){
                showMsg("请输入评论内容")
                return@setOnClickListener
            }
            lifecycleScope.launch {
                repeatOnLifecycle(Lifecycle.State.STARTED){

                }
            }
        }
    }

    override fun lazyLoad() {
        data = arguments?.getParcelable<SimpleVideoEntity>("data") as SimpleVideoEntity
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                viewModel.intents.send(CommentIntent.GetCommentInfo(0, data.item_id))
            }
        }
    }

    override fun getFragmentRootView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = HomeFragmentHomeDetailCommentBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun handleState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED){
                viewModel.state.collect{
                    when (it) {
                        //评论获取成功
                        is CommentState.CommentSuccessState -> {
                            //展示列表中
                        }

                        //评论获取失败
                        is CommentState.CommentFailedState -> {
                            showMsg(getString(R.string.home_detail_comment_nocommentvalue))
                        }

                        //评论发布成功
                        is CommentState.PublishCommentSuccess -> {
                            binding.etHomeDetailCommentContent.setText("")
                        }

                        //评论发布失败
                        is CommentState.PublishCommentFailed -> {
                            showMsg(getString(R.string.home_detail_comment_publishfailed))
                        }
                        is CommentState.Init -> {

                        }
                    }
                }
            }
        }
    }
}